const fp = require('lodash/fp')
const {Maybe, Container} =  require('./support');

//练习1：创建一个能让functor里的值增加的函数ex1
let maybe = Maybe.of([5, 6, 1])
let ex1 = (value, addValue) => value.map(fp.map(item => fp.add(item, addValue)))
console.log(ex1(maybe, 1)._value)

//练习2：实现ex2，获取列表第一个元素
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = (value) => value.map(fp.first)
console.log(ex2(xs)._value)

//练习3：实现ex3，找到user的名字的首字母
let safeProp = fp.curry(function (x, o) {
    return Maybe.of(o[x])
})
let user = { id: 2, name: 'Albert'} 
let ex3 = (user) => safeProp('name', user).map(fp.split('')).map(fp.first)
console.log(ex3(user)._value)

//练习4： 使用Maybe重写ex4
let ex4 = function(n) {
    return Maybe.of(n).map(parseInt)
}